package com.pqc.tbk.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class MSGSender {

	private static final String SEND_URL = "http://gbk.sms.webchinese.cn";
	private static final String REST_URL = "http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num";
	private static final String UID = "labreeze";
	private static final String KEY = "04b6639f4664194d4405";
	private static HttpClient client = new HttpClient();
	public static final String MANAGER_NUM = "13516713578";

	public static void main(final String[] args) throws Exception {
		System.out.println(restNum());
	}

	/**
	 * 返回参数说明:<br>
	 * -1 没有该用户账户,-2 密钥不正确（不是用户密码）,-3 短信数量不足, -4 手机号格式不正确 <br>
	 * -11用户被禁用,-14 短信内容出现非法字符, -41 手机号码为空,-42 短信内容为空 <br>
	 * 大于0 短信发送数量
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String sendMessage(final String mobile, final String content)
			throws Exception {
		final PostMethod post = new PostMethod(SEND_URL);
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
		final NameValuePair[] data = { new NameValuePair("Uid", UID),
				new NameValuePair("Key", KEY),
				new NameValuePair("smsMob", mobile),
				new NameValuePair("smsText", content) };
		post.setRequestBody(data);
		client.executeMethod(post);
		final String result = new String(post.getResponseBodyAsString()
				.getBytes("gbk"));
		post.releaseConnection();
		return result;
	}

	/**
	 * 查询短信剩余数量
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String restNum() throws Exception {
		final PostMethod post = new PostMethod(REST_URL);
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
		final NameValuePair[] data = { new NameValuePair("Uid", UID),
				new NameValuePair("Key", KEY) };
		post.setRequestBody(data);
		client.executeMethod(post);
		final String result = new String(post.getResponseBodyAsString()
				.getBytes("gbk"));
		post.releaseConnection();
		return result;
	}

}
